package net.anzix.jhacks.flexclipse.properties;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

public class ProjectPropertyLoader {
	final IProject project;

	Properties properties = null;

	public static final String flexclipsePropertyFileName = ".flexclipse";
	public static final String MAINMXML_PROPERTY = "flexclipse.main-mxml";
	public static final String DEFAULT_MAINMXML = "src/flex/Main.mxml";
	public static final String CSS_PROPERTY = "flexclipse.css";
	public static final String DEFAULT_CSS = null;
	public static final String OUTPUT_PROPERTY = "flexclipse.output";
	public static final String DEFAULT_OUTPUT = "out.swf";

	public ProjectPropertyLoader(final IProject project) {
		this.project = project;
	}

	Properties getProperties() {
		if (properties == null) {
			properties = new Properties();
			IFile propertyFile = project.getFile(flexclipsePropertyFileName);
			try {
				properties.load(propertyFile.getContents());
			} catch (IOException e) {
				//no such file? never mind!
			} catch (CoreException e) {
				//no such file? never mind!
			}
		}
		return properties;
	}

	String getProperty(final String key, final String def) {
		final String storedValue = getProperties().getProperty(key);
		return storedValue == null ? def : storedValue;
	}

	void setProperty(final String key, final String value, final String def) {
		if(def == null) {
			setNullDefProperty(key, value);
		} else {
			setNonNullDefProperty(key, value, def);
		}
	}

	private void setNonNullDefProperty(final String key, final String value,
			final String def) {
		if (def.equals(value)) {
			// keep the properties as small as possible
			// so remove the defaults
			getProperties().remove(key);
		} else {
			getProperties().setProperty(key, value);
		}
	}

	private void setNullDefProperty(final String key, final String value) {
		if (value == null) {
			// keep the properties as small as possible
			// so remove the defaults
			getProperties().remove(key);
		} else {
			getProperties().setProperty(key, value);
		}
	}

	public void persistProperties(final IProgressMonitor monitor)
			throws IOException, CoreException {
		final IFile propertyFile = project.getFile(flexclipsePropertyFileName);
		final File file = propertyFile.getRawLocation().toFile();
		final FileOutputStream outputStream = new FileOutputStream(file);
		properties.store(outputStream, "Flexclipse property file.");
		outputStream.flush();
		outputStream.close();
		//propertyFile.refreshLocal(1, monitor);
	}

	public String getMainMxml() {
		return getProperty(MAINMXML_PROPERTY, DEFAULT_MAINMXML);
	}

	public void setMainMxml(final String mainMxml) {
		setProperty(MAINMXML_PROPERTY, mainMxml, DEFAULT_MAINMXML);
	}

	public String getOutput() {
		return getProperty(OUTPUT_PROPERTY, DEFAULT_OUTPUT);
	}

	public void setOutput(final String output) {
		setProperty(OUTPUT_PROPERTY, output, DEFAULT_OUTPUT);
	}

	public String getDefaultCSS() {
		return getProperty(CSS_PROPERTY, DEFAULT_CSS);
	}

	public void setDefaultCSS(final String css) {
		setProperty(CSS_PROPERTY, css, DEFAULT_CSS);
	}
}
